package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
    @Override
    public void work(BookList booklist) {
        System.out.println("新增图书...");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入新增的图书书名:");
        String name = scanner.nextLine();
        //先检查该图书是否已存在
        int i = 0;
        int currentSize = booklist.getUsedSize();
        for (i = 0; i < currentSize; i++) {
            if(booklist.findBook(i).getName().equals(name)) {
                System.out.println("该图书已存在...");
                System.out.println("--------------------------------------");                break;
            }
        }
        if(i >= currentSize) {
            System.out.println("请输入新增图书的作者:");
            String author = scanner.nextLine();
            System.out.println("请输入新增图书的价格:");
            int price = scanner.nextInt();
            scanner.nextLine();
            System.out.println("请输入新增图书的类型:");
            String type = scanner.nextLine();
            Book book = new Book(name,author,price,type);
            booklist.setBook(currentSize,book);
            booklist.setUsedSize(currentSize+1);
            System.out.println("新增图书成功！");
            System.out.println("--------------------------------------");        }
    }
}
